﻿/*
 * Created by SharpDevelop.
 * User: rogerio
 * Date: 12/16/2009
 * Time: 1:05 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;

namespace csharp.rad.general
{
	/// <summary>
	/// Description of Commitments.
	/// </summary>
	public class Commitment : csharp.common.DefaultClass, csharp.common.IClass
	{
		#region private declarations 
		private String _date;
		private String _time;
		private String _subject;
		private String _company_id;
		private String _user_id;
		private String _resolved;
		#endregion
		
		public Commitment()
		{
			clear();
			
			gsql.table = "common.commitments";
			_sequence  = "common.commitments_commitments_id_seq";
			companyId  = Information.companyId;
			_user_id   = Information.userId;
		}
		
		public void clear()
		{
			_id         = String.Empty;
			_date       = String.Empty;
			_time       = String.Empty;
			_subject    = String.Empty;
			_user_id    = Information.userId;
			_company_id = Information.companyId;
			_resolved   = String.Empty;
		}
		
		#region properties
		public string date
		{
			get { return _date; }
			set { _date = value; }
		}
		
		public string time
		{
			get {return _time;}
			set {_time = value;}
		}
		
		public string subject
		{
			get {return _subject;}
			set {_subject = value;}
		}
		
		public string companyId
		{
			get {return _company_id;}
			set {_company_id = value;}
		}
		
		public string user_id
		{
			get {return _user_id;}
			set {_user_id = value;}
		}
		
		public string Resolved {
			get { return _resolved; }
			set { _resolved = value; }
		}
		
		#endregion
		
		public override bool delete()
		{
			return base.delete();
		}
		
		public override void get(System.Data.DataRow dr)
		{
			_id         = dr["commitments_id"].ToString();
			_date       = Convert.ToDateTime(dr["commitments_date"]).ToString("dd/MM/yyyy");
			_time       = Convert.ToDateTime(dr["commitments_time"]).ToString("HH:mm");
			_subject    = dr["commitments_subject"].ToString();
			_company_id = dr["commitments_company_id"].ToString();
			_user_id    = dr["commitments_user_id"].ToString();
			_resolved   = dr["commitments_resolved"].ToString();
		}
		
		public override bool save()
		{
			return base.save();
		}
		
		public override void setGsql()
		{
			gsql.setValue(_id         , "commitments_id");
			gsql.setValue(Convert.ToDateTime(_date).ToString("yyyy/MM/dd")       , "commitments_date");
			gsql.setValue(_time       , "commitments_time");
			gsql.setValue(_subject    , "commitments_subject");
			gsql.setValue(_company_id , "commitments_company_id");
			gsql.setValue(_user_id    , "commitments_user_id");
			gsql.setValue(_resolved   , "commitments_resolved");
		}
		
		public override bool store(csharp.common.GenerateSql.TCommandType ctype)
		{
			return base.store(ctype);
		}
		
		public override bool update()
		{
			return base.update();
		}
		
		
	}
	
	public class Commitments : csharp.common.DefaultList, csharp.common.IList
	{
		public Commitments()
		{
			gsql.table = "common.commitments";
		}
		
		
		public Commitment get(Int32 index)
		{
			return (Commitment)items[index];
		}
		
		public Commitment get()
		{
			return (Commitment)items[index];
		}
		
		public bool fillList()
		{
			gsql.prepare();
			gsql.setValue("","*");
			gsql.conditionalWhere = "commitments_company_id = "+Information.companyId;
			gsql.conditionalWhere = "commitments_user_id = '"+Information.userId+"'";
			gsql.conditionalWhere = "NOT commitments_resolved";
			
			return mountList();
		}
		
		public bool fillListByMonth(string month, string year, bool returnResolved)
		{
			gsql.prepare();
			gsql.setValue("","*");
			gsql.conditionalWhere = "commitments_company_id = "+Information.companyId;
			gsql.conditionalWhere = "commitments_user_id = '"+Information.userId+"'";
			gsql.conditionalWhere = "EXTRACT(MONTH FROM commitments_date) = '"+month+"'";
			gsql.conditionalWhere = "EXTRACT(YEAR FROM commitments_date) = '"+year+"'";
			if(!returnResolved)
			{
				gsql.conditionalWhere = " NOT commitments_resolved";
			}
			return mountList();
		}
		
		public bool fillListByDay(string day, bool returnResolved )
		{
			gsql.prepare();
			gsql.setValue("","*");
			gsql.conditionalWhere = "commitments_company_id = "+Information.companyId;
			gsql.conditionalWhere = "commitments_user_id = '"+Information.userId+"'";
			gsql.conditionalWhere = "commitments_date = '"+day+"'";
			if(!returnResolved)
			{
				gsql.conditionalWhere = " NOT commitments_resolved";
			}
			
			return mountList();
		}
		
		private bool mountList()
		{
			Int32 tam;
			
			if (!gsql.execute())
				return false;
			
			items.Clear();
			tam= gsql.getRowCount();
			for (int i=0; i < tam; i++)
			{
				Commitment c = new Commitment();
				c.get(gsql.getRow(i));
				items.Add(c);
			}
			return true;
		}
	}
}
